package com.zdx.yswl.ui.tool;

import java.util.ArrayList;
import java.util.List;

import com.zdx.yswl.App;
import com.zdx.yswl.ui.bean.Device;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SynchroData extends Service
{
    // 用户名
    private String userName;

    // 设备
    private Device deviceT;
    private List<Device> listDeviceFromSql = new ArrayList<Device>();

    // 初始化Handler
    public BindDevHandler bindDevHandler;

    // 数据库
    private SqliteDevice sqliteDevice;

    // 处理线程
    private Thread thread;

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e("服务", "服务启动");
        bindDevHandler = new BindDevHandler();

        userName = intent.getStringExtra("userName");

        sqliteDevice = new SqliteDevice(SynchroData.this);

        thread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                Log.e("线程", "线程开始");
                while(!thread.isInterrupted())
                {
                    listDeviceFromSql.addAll(sqliteDevice.getLsitDevFromSql());

                    for(int i = 0; i < listDeviceFromSql.size(); i++)
                    {
                        if(listDeviceFromSql.get(i).getBind() == false)
                        {
                            deviceT = listDeviceFromSql.get(i);
                            HttpBindDeviceThread httpBindDeviceThread = new HttpBindDeviceThread(
                                    userName,
                                    listDeviceFromSql.get(i).getFullNo(),
                                    bindDevHandler,
                                    listDeviceFromSql.get(i).getTag());
                            httpBindDeviceThread.start();
                        }
                    }

                    long startTime = System.currentTimeMillis();
                    while(System.currentTimeMillis() - startTime < 30000)
                    {
                    }
                }
                Log.e("线程", "线程结束");
            }
        });
        thread.start();

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        thread.interrupt();
        Log.e("服务", "服务关闭");
    }

    /** 绑定设备Handler **/
    private class BindDevHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);

            Bundle bundle = msg.getData();

            int code = bundle.getInt("code");
            Log.e("code bind", "" + code);

            // String tag = bundle.getString("tag");

            if(code == 200 && bundle.getBoolean("status"))
            {
                // 绑定成功， 将绑定位标记为绑定
                // sqliteDevice.addDevToSql(devicename, 1, tag);
                // HttpGetDeviceThread threadATemp = new HttpGetDeviceThread(userName, getDevHandler);
                // listDevice.clear(); // 先清除链表中的设备
                // threadATemp.start();
                sqliteDevice.addDevToSql(deviceT.getFullNo(), 1, deviceT.getTag());
            }
            else
            {
                // // 绑定失败，继续申请绑定
                // HttpBindDeviceThread httpBindDeviceThread =
                        // new HttpBindDeviceThread(userName, devicename, bindDevHandler, tag);
                // httpBindDeviceThread.start();
            }
        }
    }
}